﻿package com.mirror.llk.util;

import com.mirror.frame.exception.ServiceException;
import com.mirror.frame.log4j.ILog;
import com.mirror.frame.log4j.Logger;
import com.thoughtworks.xstream.XStream;

/**
 * @Title: XStream 工具 接口输入输出参数 XML与BEAN转换 XML的解析支撑类
 * @Copyright: Copyright (c) 2012-7-30
 * @Company: 幻方朗睿
 * @Author: Andy.D
 * @Version: 1.0
 */
public final class XStreamUtils {

    @Logger
    private static ILog log;

    /**
     * <默认构造函数>
     */
    private XStreamUtils() {
    }

    /**
     * xml类型转换成Object类型
     * 
     * @param xml
     * @param objClass
     * @return 转换后的对象
     */
    public static Object parseXmlToObj(String xml, Class<?> objClass) {

        log.info("请求报文体为:" + xml);

        Object obj = null;
        try {
            XStream xstream = new XStream();
            // 显示声明使用注解
            xstream.processAnnotations(objClass);
            xstream.autodetectAnnotations(true);
            obj = xstream.fromXML(xml);
        }
        catch (Exception e) {
            throw new ServiceException(e, Interface.ERROR_CODE_9998);
        }
        return obj;
    }

    /**
     * Object类型转换成xml
     * 
     * @param out
     * @return String 输入xml
     */
    public static String parseObjToXml(Object out) {
        String str = null;
        try {
            XStream xstream = new XStream();
            // 显示声明使用注解
            xstream.processAnnotations(out.getClass());
            xstream.autodetectAnnotations(true);
            str = xstream.toXML(out);
            str = Constant.HTTP_XML.XML_HEAD + str;
        }
        catch (Exception e) {
            throw new ServiceException(e, Interface.ERROR_CODE_9998);
        }
        log.info("返回报文体为:" + str);
        return str;
    }
}